namespace DotNetNuke.Security.Permissions.Controls
{
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CheckBoxTemplate : ITemplate
    {
        private static List<WeakReference> __ENCList = new List<WeakReference>();
        private bool mAutoPostBack;
        private string mDataField;
        private bool mReadOnly;
        private string mTrueValue;

        public event BoxCheckedChangedEventHandler BoxCheckedChanged;

        public CheckBoxTemplate(bool editable)
        {
            List<WeakReference> refObjectHelperL0 = __ENCList;
            lock (refObjectHelperL0)
            {
                __ENCList.Add(new WeakReference(this));
            }
            this.mAutoPostBack = false;
            this.mReadOnly = Conversions.ToBoolean(Interaction.IIf(editable, false, true));
        }

        public CheckBoxTemplate(bool editable, string trueValue)
        {
            List<WeakReference> refObjectHelperL0 = __ENCList;
            lock (refObjectHelperL0)
            {
                __ENCList.Add(new WeakReference(this));
            }
            this.mAutoPostBack = false;
            this.mReadOnly = Conversions.ToBoolean(Interaction.IIf(editable, false, true));
            this.mTrueValue = trueValue;
        }

        private void BindData(object sender, EventArgs e)
        {
            CheckBox box = (CheckBox) sender;
            DataGridItem container = (DataGridItem) box.NamingContainer;
            box.Checked = false;
            string data = ((DataRowView) container.DataItem)[this.mDataField].ToString();
            if (data.EndsWith("N"))
            {
                this.mReadOnly = true;
            }
            else
            {
                this.mReadOnly = false;
            }
            if (data.StartsWith("True") | data.EndsWith("N"))
            {
                box.Checked = true;
            }
            box.Enabled = !this.mReadOnly;
            box.ID = data;
        }

        public void InstantiateIn(Control container)
        {
            CheckBox box = new CheckBox();
            box.DataBinding += new EventHandler(this.BindData);
            box.AutoPostBack = this.AutoPostBack;
            box.CheckedChanged += new EventHandler(this.OnCheckChanged);
            container.Controls.Add(box);
        }

        public void OnCheckChanged(object sender, EventArgs e)
        {
            CheckBox box = (CheckBox) sender;
            DataGridItem container = (DataGridItem) box.NamingContainer;
            int gridIndex = container.ItemIndex;
            BoxCheckedChangedEventHandler refObjectHelperS0 = this.BoxCheckedChanged;
            if (refObjectHelperS0 != null)
            {
                refObjectHelperS0(gridIndex, box.Checked);
            }
        }

        public bool AutoPostBack
        {
            get
            {
                return this.mAutoPostBack;
            }
            set
            {
                this.mAutoPostBack = value;
            }
        }

        public string DataField
        {
            get
            {
                return this.mDataField;
            }
            set
            {
                this.mDataField = value;
            }
        }

        public delegate void BoxCheckedChangedEventHandler(int gridIndex, bool @checked);
    }
}

